package ar.edu.utn.frsf.cidisi.upcolbpip2pn.generator.petrinet;

import org.eclipse.emf.common.util.EList;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.*;;

public class If2PN
{
  protected static String nl;
  public static synchronized If2PN create(String lineSeparator)
  {
    nl = lineSeparator;
    If2PN result = new If2PN();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = NL + "\t";
  protected final String TEXT_2 = NL + "    <page id=\"";
  protected final String TEXT_3 = "\">" + NL + "      <pageattr name=\"";
  protected final String TEXT_4 = "\"/>" + NL + "      " + NL + "\t";
  protected final String TEXT_5 = NL + "\t\t";
  protected final String TEXT_6 = NL + "\t\t";
  protected final String TEXT_7 = NL + "\t\t";
  protected final String TEXT_8 = NL + "\t\t";
  protected final String TEXT_9 = NL + "\t\t";
  protected final String TEXT_10 = NL + "\t\t";
  protected final String TEXT_11 = NL + "\t\t";
  protected final String TEXT_12 = NL + "\t\t";
  protected final String TEXT_13 = NL + "    ";
  protected final String TEXT_14 = NL + "\t\t";
  protected final String TEXT_15 = NL + "\t\t";
  protected final String TEXT_16 = NL + "\t\t";
  protected final String TEXT_17 = NL + "\t\t";
  protected final String TEXT_18 = NL + "\t\t";
  protected final String TEXT_19 = NL + "\t\t";
  protected final String TEXT_20 = NL + "    " + NL + "        </page>";
  protected final String TEXT_21 = NL;

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
     If controlFlowSegment = (If) argument; 
    stringBuffer.append(TEXT_1);
    GlobalValues.pageId++;
    stringBuffer.append(TEXT_2);
    stringBuffer.append( "ID"+GlobalValues.pageId);
    stringBuffer.append(TEXT_3);
    stringBuffer.append( "If"+"ID"+GlobalValues.pageId);
    stringBuffer.append(TEXT_4);
    
	Place2PN place2pn = Place2PN.create(null);
	
	Place inputPlace = null;
	
	SubPage sp = GlobalValues.subpagesMap.get(controlFlowSegment);
	if (sp!=null){
		inputPlace = sp.getSubnetInput();
		if (inputPlace==null){
			inputPlace = new Place(controlFlowSegment.getName());
		}
		
    stringBuffer.append(TEXT_5);
    stringBuffer.append( place2pn.generate(inputPlace));
    stringBuffer.append(TEXT_6);
    
	} else{
		inputPlace = new Place(controlFlowSegment.getName());
		
    stringBuffer.append(TEXT_7);
    stringBuffer.append( place2pn.generate(inputPlace));
    stringBuffer.append(TEXT_8);
    
	}
	
	
	Place outputPlace = null;
	
	sp = GlobalValues.subpagesMap.get(controlFlowSegment);
	if (sp!=null){
		outputPlace = sp.getSubnetOutput();
		if (outputPlace==null){
			outputPlace = new Place(controlFlowSegment.getName());
		}
		
    stringBuffer.append(TEXT_9);
    stringBuffer.append( place2pn.generate(outputPlace));
    stringBuffer.append(TEXT_10);
    
	} else{
		outputPlace = new Place(controlFlowSegment.getName());
		
    stringBuffer.append(TEXT_11);
    stringBuffer.append( place2pn.generate(outputPlace));
    stringBuffer.append(TEXT_12);
    
	}


    stringBuffer.append(TEXT_13);
    
	Transition2PN transition2pn = Transition2PN.create(null);
	Arc2PN arc2PN = Arc2PN.create(null);

	EList<InteractionPath> interactionPaths = controlFlowSegment.getInteractionPath();
	for (InteractionPath interactionPath : interactionPaths){
	
		Transition transition = new Transition("cond"+interactionPath.getName());
	
		Arc inputArc = new Arc(inputPlace, transition, Arc.PLACE_TO_TRANSITION);
		
    stringBuffer.append(TEXT_14);
    stringBuffer.append( arc2PN.generate(inputArc));
    stringBuffer.append(TEXT_15);
    	
		Arc outputArc = new Arc(outputPlace, transition, Arc.TRANSITION_TO_PLACE);
		
    stringBuffer.append(TEXT_16);
    stringBuffer.append( arc2PN.generate(outputArc));
    stringBuffer.append(TEXT_17);
    			
		
		SubPage subPage = new SubPage(interactionPath.getName(), inputPlace, outputPlace);
		transition.setSubPage(subPage);
		GlobalValues.subpagesMap.put(interactionPath,subPage);
			
		Instance newInstance = new Instance(transition);
		Instances instances = Instances.create(null);
		GlobalValues.instancesStr = GlobalValues.instancesStr + instances.generate(newInstance);
		
		
		InteractionPath2PN interactionPath2pn = InteractionPath2PN.create(null);
		String interactionPath2pnStr = interactionPath2pn.generate(interactionPath);
		GlobalValues.pages.put(interactionPath, interactionPath2pnStr);
		
		
		GlobalValues.instancesStr = GlobalValues.instancesStr + "</instance>";
		GlobalValues.instancesMap.put(interactionPath, GlobalValues.instancesStr);
				
		
    stringBuffer.append(TEXT_18);
    stringBuffer.append( transition2pn.generate(transition));
    stringBuffer.append(TEXT_19);
    
			
	}  
    
    stringBuffer.append(TEXT_20);
    stringBuffer.append(TEXT_21);
    return stringBuffer.toString();
  }
}
